package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.StudentGrade;
import com.xmy.cultivate.entity.StudentGradeContinue;
import com.xmy.cultivate.mapper.StudentGradeContinueMapper;
import com.xmy.cultivate.service.IStudentGradeContinueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 学生续报 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2025-06-19
 */
@Service
public class StudentGradeContinueServiceImpl extends ServiceImpl<StudentGradeContinueMapper, StudentGradeContinue> implements IStudentGradeContinueService {


    @Override
    public void addData(StudentGrade studentGrade, Integer isPresort) {
        if (studentGrade.getLessonType().equals(1)){
            LocalDate dateNow = LocalDate.now();
            QueryWrapper<StudentGradeContinue> wrapper = new QueryWrapper<>();
            wrapper.eq("year_part", studentGrade.getYearPart());
            wrapper.eq("quarter_num", studentGrade.getQuarterNum());
            wrapper.eq("student_id", studentGrade.getStudentId());
            wrapper.eq("subjects_id", studentGrade.getSubjectsId());
            wrapper.eq("lesson_type", studentGrade.getLessonType());
            wrapper.le("start_date",dateNow);
            wrapper.ge("end_date",dateNow);
            Long count = this.count(wrapper);
            if (count == 0L){
                StudentGradeContinue studentGradeContinue = new StudentGradeContinue();
                studentGradeContinue.setYearPart(studentGrade.getYearPart());
                studentGradeContinue.setQuarterNum(studentGrade.getQuarterNum());
                studentGradeContinue.setSchoolId(studentGrade.getSchoolId());
                studentGradeContinue.setGradeId(studentGrade.getGradeId());
                studentGradeContinue.setCourseId(studentGrade.getCourseId());
                studentGradeContinue.setStudentId(studentGrade.getStudentId());
                studentGradeContinue.setLessonType(studentGrade.getLessonType());
                studentGradeContinue.setSubjectsId(studentGrade.getSubjectsId());
                studentGradeContinue.setYearClassId(studentGrade.getYearClassId());
                studentGradeContinue.setIsPresort(isPresort);
                studentGradeContinue.setStartDate(dateNow);
                String dateStr = "2099-12-31";
                // 直接解析 ISO 格式的日期字符串
                LocalDate endDate = LocalDate.parse(dateStr);
                studentGradeContinue.setEndDate(endDate);
                this.save(studentGradeContinue);
            }else {
                List<StudentGradeContinue> list = this.list(wrapper);
                for (StudentGradeContinue studentGradeContinue : list){
                    //如果失败，调整为1
                    if (studentGradeContinue.getIsPresort().equals(4)){
                        studentGradeContinue.setIsPresort(isPresort);
                        this.updateById(studentGradeContinue);
                    }
                }
            }
        }
    }

    @Override
    public void changeEndDate(StudentGrade studentGrade){
        LocalDate dateNow = LocalDate.now();
        QueryWrapper<StudentGradeContinue> wrapper = new QueryWrapper<>();
        wrapper.eq("year_part", studentGrade.getYearPart());
        wrapper.eq("quarter_num", studentGrade.getQuarterNum());
        wrapper.eq("student_id", studentGrade.getStudentId());
        wrapper.eq("subjects_id", studentGrade.getSubjectsId());
        wrapper.eq("lesson_type", studentGrade.getLessonType());
        wrapper.le("start_date",dateNow);
        wrapper.ge("end_date",dateNow);
        List<StudentGradeContinue> list = this.list(wrapper);
        for (StudentGradeContinue studentGradeContinue : list){
            LocalDate afterDate = dateNow.minusDays(1);
            if (studentGradeContinue.getStartDate().isAfter(afterDate)){
                this.removeById(studentGradeContinue);
            }else {
                studentGradeContinue.setEndDate(afterDate);
                this.updateById(studentGradeContinue);
            }
        }
    }

    @Override
    public void changeFail(StudentGrade studentGrade, Integer isPresort){
        LocalDate dateNow = LocalDate.now();
        QueryWrapper<StudentGradeContinue> wrapper = new QueryWrapper<>();
        wrapper.eq("year_part", studentGrade.getYearPart());
        wrapper.eq("quarter_num", studentGrade.getQuarterNum());
        wrapper.eq("student_id", studentGrade.getStudentId());
        wrapper.eq("subjects_id", studentGrade.getSubjectsId());
        wrapper.eq("lesson_type", studentGrade.getLessonType());
        wrapper.le("start_date",dateNow);
        wrapper.ge("end_date",dateNow);
        List<StudentGradeContinue> list = this.list(wrapper);
        for (StudentGradeContinue studentGradeContinue : list){
            LocalDate afterDate = dateNow.minusDays(1);
            if (studentGradeContinue.getStartDate().isAfter(afterDate)){
                this.removeById(studentGradeContinue);
            }else {
                studentGradeContinue.setIsPresort(isPresort);
                studentGradeContinue.setEndDate(afterDate);
                this.updateById(studentGradeContinue);
            }
        }
    }
}
